Imports System.Collections.Generic

Partial Class ChangePassword
    Inherits System.Web.UI.Page

    Protected userExists As Boolean
    Protected passwordConfig As Cdm.DataCore.LookupTables.LuPasswordConfiguration

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        passwordConfig = Cdm.DataCore.LookupTables.LuPasswordConfiguration.Retrieve()
        userExists = (Session("LoginId") IsNot Nothing)
        Me.ErrorLabel.Visible = False
        Me.SuccessPanel.Visible = False

        If userExists Then
            Me.DisableTextBox(Me.UserIdTextBox)
            Me.UserIdTextBox.Text = Session("LoginId")
        Else
            Me.OldPwdRequiredFieldValidator.EnableClientScript = False
            Me.DisableTextBox(Me.OldPasswordTextBox)
            Me.UserIdTextBox.ReadOnly = False
        End If

        Me.UserIdTextBox.MaxLength = passwordConfig.UseridLength
        Me.OldPasswordTextBox.MaxLength = passwordConfig.PasswordLength
        Me.NewPasswordTextBox.MaxLength = passwordConfig.PasswordLength
        Me.ConfirmNewPasswordTextBox.MaxLength = passwordConfig.PasswordLength

        If Not Page.IsPostBack Then

            'Application.Clear()
            If Application("CurrentLogo") Is Nothing Then
                Dim appSettings As List(Of Cdm.DataCore.LookupTables.LuApplicationSettings)
                appSettings = Cdm.DataCore.LookupTables.LuApplicationSettings.ListAll()

                For i As Integer = 0 To appSettings.Count - 1
                    Application(appSettings.Item(i).SettingName) = appSettings.Item(i).SettingValue
                Next
            End If

            Me.imgLogo.ImageUrl = FullPath(Application("CurrentLogo"))
            Me.imgLogo.AlternateText = Application("StateDeptName")
            Me.AppNameLabel.Text = Application("AppName")
            Me.DeptNameLabel.Text = Application("StateDeptName")

        End If

    End Sub

    Protected Sub ChangePasswordButton_Click(ByVal sender As Object, _
                                             ByVal e As System.EventArgs) _
                                             Handles ChangePasswordButton.Click

        ' Determine that the new and confirmed new password field values match.
        If Me.NewPasswordTextBox.Text = Me.ConfirmNewPasswordTextBox.Text Then
            Dim loginResult As UserAuthorization.LoginStatus = UserAuthorization.LoginUser(Me.UserIdTextBox.Text, Me.OldPasswordTextBox.Text)
            ' Check if the user is an expired user or a new user.
            ' Display error message.
            
            If loginResult = UserAuthorization.LoginStatus.NotAuthenticated Then
                Me.ErrorLabel.Visible = True
                Me.ErrorLabel.Text = "Current userid/password not found."
            ElseIf loginResult = UserAuthorization.LoginStatus.Expired Then

                ' Get a reference to the expired user and reset his/her password and expiration date.
                Dim user As Cdm.DataCore.UserSecurity.CDMUser = Cdm.DataCore.UserSecurity.CDMUser.Retrieve(Session("CurrentUserId"))
                user.Password = Me.NewPasswordTextBox.Text
                user.PasswordExpires = Now.AddDays(passwordConfig.ExpirePeriod)
                UserAuthorization.CurrentUser.PasswordExpires = Now.AddDays(passwordConfig.ExpirePeriod)
                If UserAuthorization.CurrentUser IsNot Nothing Then
                    user.LastModified = UserAuthorization.CurrentUser.LogonId
                End If
                user.DateModified = Now
                user.Persist()

                Me.SuccessPanel.Visible = True
                Me.MessageLabel.Text = "Password changed successfully."
                HideChangePasswordRows()

            Else

                ' Get a reference to the new user and reset his/her password and expiration date.
                Dim newUser As Cdm.DataCore.UserSecurity.CDMUser = Cdm.DataCore.UserSecurity.CDMUser.Retrieve(Me.UserIdTextBox.Text)
                If newUser IsNot Nothing Then

                    newUser.Password = Me.NewPasswordTextBox.Text
                    newUser.PasswordExpires = Now.AddDays(passwordConfig.ExpirePeriod)
                    If UserAuthorization.CurrentUser IsNot Nothing Then
                        newUser.LastModified = UserAuthorization.CurrentUser.LogonId
                    End If
                    newUser.DateModified = Now
                    newUser.Persist()

                    Me.SuccessPanel.Visible = True
                    Me.MessageLabel.Text = "Password changed successfully."
                    HideChangePasswordRows()
                End If

            End If

        Else

            ' Display error message.
            Me.ErrorLabel.Visible = True
            Me.ErrorLabel.Text = "Confirmation password must match new password."

        End If

    End Sub

    Private Sub HideChangePasswordRows()
        Me.trChangePassword.Visible = False
        Me.trConfirmPassword.Visible = False
        Me.trNewPassword.Visible = False
        Me.trOldPassword.Visible = False
    End Sub

    Private Function FullPath(ByVal pathName As String) As String

        If pathName Is Nothing Then
            Return ""
        End If

        If pathName.StartsWith("~") Then
            If pathName.Length > 1 Then
                Return HttpContext.Current.Request.ApplicationPath & pathName.Substring(1)
            Else
                Return HttpContext.Current.Request.ApplicationPath
            End If

        Else
            Return pathName
        End If

    End Function

    Private Sub DisableTextBox(ByVal disabledTextBox As TextBox)

        With disabledTextBox
            .ReadOnly = True
            .BorderStyle = BorderStyle.Solid
            .BorderWidth = Unit.Pixel(0)
            .BorderColor = Drawing.Color.LightGray
            .BackColor = Drawing.Color.WhiteSmoke
        End With

    End Sub

    Protected Sub ProceedButton_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles ProceedButton.Click

        Response.Redirect("Default.aspx")

    End Sub

End Class
